package com.dempsey.voronoiweb.project.entity;

import lombok.Data;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Package: pojo
 * Description：
 * Author: Dempsey
 * Date:  2020/3/7 15:54
 * Modified By:
 */

@Data
public class Cover {

    Point position;//基站位

    List<Point> broderSites = new ArrayList<>();//边缘点集

    List<Edge> coverEdges = new ArrayList<>();//覆盖区的边集

    //构造器
    public Cover(Point point, List<Point> broderSites, List<Edge> coverEdges) {
        this.position = point;
        this.broderSites = broderSites;
        this.coverEdges = coverEdges;
        sortSite();
    }

    //边缘点集顺时针或逆时针排序
    private void sortSite() {//别嫌长，从另外一个函数里截取过来的，就这两个地方要用，懒得做复用了
        List<Edge> allBroderEdges = coverEdges;//所有边缘边的集合
        LinkedList<Edge> broderEdges = new LinkedList<>();//有顺序的边缘边集合
        //将边缘边按序排列
        for (int i = 0; i < allBroderEdges.size() - 1; i++) {
            //插入第一个
            if (i == 0) {
                broderEdges.add(allBroderEdges.get(i));
            }
            Edge lastEdge = broderEdges.getLast();
            //遍历插入
            for (Edge edge : allBroderEdges) {
                if (!edge.equals(lastEdge)) {//在两边不相同的情况下
                    if (lastEdge.getB().equals(edge.getA())) {//若链表中最后一边的Bid=另一条边的Aid
                        broderEdges.addLast(edge);//把该边塞到链表尾
                        break;
                    }
                    if (lastEdge.getB().equals(edge.getB())) {//若链表中最后一边的Bid=另一条边的Bid
                        Point tempPoint = edge.getB();
                        edge.setB(edge.getA());
                        edge.setA(tempPoint);//把该边的两端换个位置再塞到链表尾
                        broderEdges.addLast(edge);//把该边塞到链表尾
                        break;
                    }
                }
            }
        }
        broderSites.clear();
        for (Edge edge : broderEdges) {
            broderSites.add(edge.getA());
        }

    }
}
